package cn.itcast.goods.controller;

import cn.itcast.goods.dto.SetmealDTO;
import cn.itcast.goods.dto.SetmealPageQueryDTO;
import cn.itcast.goods.mapper.SetmealMapper;
import cn.itcast.goods.pojo.Setmeal;
import cn.itcast.goods.service.SetmealService;
import cn.itcast.goods.vo.DishItemVO;
import cn.itcast.goods.vo.SetmealOverViewVO;
import cn.itcast.goods.vo.SetmealVO;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"/admin/setmeal","/user/setmeal"})
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询不同状态下套餐的数量
     *
     * @return
     */
    @GetMapping("/feign/getPackageOverview")
    public SetmealOverViewVO getPackageOverview(){
        return setmealMapper.getPackageOverview();
    }


    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询套餐")
    @GetMapping("/list")
    public Result<List> getByCategoryId(Integer categoryId){
        log.info("根据分类id查询套餐：{}",categoryId);
        List<Setmeal> setmeals = setmealService.getByCategoryId(categoryId);

        return Result.success(setmeals);
    }

    /**
     * 根据套餐id查询包含的菜品
     *
     * @return
     */
    @ApiOperation("根据套餐id查询包含的菜品")
    @GetMapping("/dish/{id}")
    public Result<List> getDishItemBySetmealId(@PathVariable Integer id){
        log.info("根据套餐id查询包含的菜品：{}",id);
        List<DishItemVO> dishItemVOS = setmealService.getDishItemBySetmealId(id);

        return Result.success(dishItemVOS);
    }

    //===================

    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageBean> pageQuery(SetmealPageQueryDTO dto){
        log.info("套餐分页查询：{}",dto);
        PageBean pageBean = setmealService.pageQuery(dto);

        return Result.success(pageBean);
    }


    /**
     * 新增套餐
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    public Result<String> add(@RequestBody SetmealDTO dto){
        log.info("新增套餐:{}",dto);
        setmealService.add(dto);

        return Result.success();
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result<String> delete(@RequestParam("ids")List<Integer> ids){
        log.info("批量删除套餐：{}",ids);
        setmealService.delete(ids);

        return Result.success();
    }

    /**
     * 修改套餐
     *
     * @param dto
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    public Result<String> update(@RequestBody SetmealDTO dto){
        setmealService.update(dto);

        return Result.success();
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Integer id){
        SetmealVO setmealVO = setmealService.getById(id);

        return Result.success(setmealVO);
    }

    /**
     * 套餐起售、停售
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(Long id, @PathVariable Integer status){
        setmealService.startOrStop(id,status);

        return Result.success();
    }
}